package org.glickr.api.photos

import org.glickr.api.FlickrApiMethod
import org.glickr.FlickrPhoto
import groovy.util.slurpersupport.GPathResult
import org.glickr.FlickrServicePluginException
import org.glickr.FlickrServiceApiException

/* ----------------------------------------------------------------------------------------------------
REQUEST: flickr.photos.getInfo
INFO: Get information about a photo. The calling user must have permission to view the photo.

AUTHENTICATION: This method does not require authentication.

ARGUMENTS:
api_key (Required): Your API application key.
photo_id (Required): The id of the photo to get information for.
secret (Optional): The secret for the photo. If the correct secret is passed then permissions checking is skipped. This enables the 'sharing' of individual photos by passing around the id and secret.

EXAMPLE RESPONSE:
<photo id="2733" secret="123456" server="12" isfavorite="0" license="3" rotation="90" originalsecret="1bc09ce34a" originalformat="png">
  <owner nsid="12037949754@N01" username="Bees" realname="Cal Henderson" location="Bedford, UK" />
  <title>orford_castle_taster</title>
  <description>hello!</description>
  <visibility ispublic="1" isfriend="0" isfamily="0" />
  <dates posted="1100897479" taken="2004-11-19 12:51:19" takengranularity="0" lastupdate="2004-08-20T10:21:09-07:00" />
  <permissions permcomment="3" permaddmeta="2" />
  <editability cancomment="1" canaddmeta="1" />
  <comments>1</comments>
  <notes>
    <note id="313" author="12037949754@N01" authorname="Bees" x="10" y="10" w="50" h="50">foo</note>
  </notes>
  <tags>
    <tag id="1234" author="12037949754@N01" raw="woo yay">wooyay</tag>
    <tag id="1235" author="12037949754@N01" raw="hoopla">hoopla</tag>
  </tags>
  <urls>
    <url type="photopage">http://www.flickr.com/photos/bees/2733/</url>
  </urls>
</photo>
The <permissions> element is only returned for photos owned by the calling user. The isfavorite attribute only makes sense for logged in users who don't own the photo. The rotation attribute is the current clockwise rotation, in degrees, by which the smaller image sizes differ from the original image.

The <date> element's lastupdate attribute is a Unix timestamp indicating the last time the photo, or any of its metadata (tags, comments, etc.) was modified.
---------------------------------------------------------------------------------------------------- */

class photosGetInfo implements FlickrApiMethod{
    //
    //  API METHOD
    //
    static final String apiMethod = 'flickr.photos.getInfo'

    //
    //  VALIDATOR
    //
    Closure validatorClosure = { FlickrPhoto photo ->
        if (!photo || !photo?.id) { return [validated:false, message:"Required value 'FlickrPhoto.id' is missing"] }
        return [validated:true]
    }

    //
    //  PARAMS
    //
    Closure paramsClosure = { FlickrPhoto photo ->
        [photo_id:photo?.id, secret:(photo?.secret ?:'')]
    }

    //
    //  PROCESSOR
    //
    Closure processorClosure = { GPathResult response, FlickrPhoto photo ->

        photo.isFavorite = (response.photo.@isFavorite?.toString() == '1')
        photo.isPublic = (response.photo.visibility.@ispublic?.toString() == '1')
        photo.isFriend = (response.photo.visibility.@isfriend?.toString() == '1')
        photo.isFamily = (response.photo.visibility.@isfamily?.toString() == '1')
        photo.secret = response.photo.@secret.toString()
        photo.license = response.photo.@license.toInteger()  // todo Enum ?
        photo.serverId = response.photo.@server.toInteger()
        photo.rotation = response.photo.@rotation.toInteger()
        photo.title = response.photo.title.toString()
        photo.description = response.photo.description.toString()
        photo.originalSecret = response.photo.@originalsecret.toString()
        photo.originalFormat = response.photo.@originalformat.toString()
//        if (response.photo.permissions) {
//            photo.permComment = response.photo.permissions.@permcomment.toInteger()     // todo Enum ?
//            photo.permAddMeta = response.photo.permissions.@permaddmeta.toInteger()     // todo Enum ?
//        }
        photo.canComment = (response.photo.editability.@cancomment.toString() == '1')
        photo.canAddMeta = (response.photo.editability.@canaddmeta.toString() == '1')
        photo.datePosted            = response.photo.dates.@posted.toString()               // todo convert to date ?
        photo.dateTaken             = response.photo.dates.@taken.toString()                // todo convert to date ?
        photo.dateTakenGranularity  = response.photo.dates.@takengranularity.toInteger()    // todo enum ?
        photo.dateLastUpdate        = response.photo.dates.@lastupdate.toString()           // todo convert to date ?

        return photo
    }

    //
    //  ERROR CODES
    //
    Closure errorsClosure = { GPathResult response ->
        if (response.err.@code.toInteger() == 1) { return null }      // photo not found

        // recoverable errors
        if ([100,105,116].contains(response.err.@code.toInteger())) {
            return new FlickrServiceApiException(response)
        } else {
            return new FlickrServicePluginException(response)
        }
    }
}
